﻿<MudPopoverProvider />

<MudDataGrid @ref="_dataGrid" Items="@_items" Filterable="true" DragDropColumnReordering="true" FilterMode="DataGridFilterMode.ColumnFilterRow" FilterCaseSensitivity="DataGridFilterCaseSensitivity.CaseInsensitive">
    <ToolBarContent>
        <MudButton OnClick="SwitchNameAgeCols" Class="switch-button">Switch Name and Age</MudButton>
    </ToolBarContent>
    <Columns>
        <PropertyColumn Property="x => x.Name" />
        <PropertyColumn Property="x => x.Age" />
        <PropertyColumn Property="x => x.Status" />
        <PropertyColumn Property="x => x.Hired" />
        <PropertyColumn Property="x => x.HiredOn" />
        <PropertyColumn Property="x => x.ApplicationId" />
    </Columns>
</MudDataGrid>

@code {
    private MudDataGrid<Model> _dataGrid = null!;

    private readonly IEnumerable<Model> _items = new List<Model>
    {
        new("Sam", 56, Severity.Normal, false, null, null),
        new("Alicia", 54, Severity.Info, null, null, new Guid("b7accfd5-928d-48ad-8a0c-9448ae37bf96")),
        new("Ira", 27, Severity.Success, true, new DateTime(2011, 1, 2), null),
        new("John", 32, Severity.Warning, false, null, null)
    };

    public record Model (string Name, int? Age, Severity? Status, bool? Hired, DateTime? HiredOn, Guid? ApplicationId);

    public void SwitchNameAgeCols()
    {
        var nameCol = _dataGrid.RenderedColumns.FirstOrDefault(x => x.PropertyName == "Name");
        var nameIndex = _dataGrid.RenderedColumns.IndexOf(nameCol);
        var ageCol = _dataGrid.RenderedColumns.FirstOrDefault(x => x.PropertyName == "Age");
        var ageIndex = _dataGrid.RenderedColumns.IndexOf(ageCol);

        _dataGrid.RenderedColumns.Remove(ageCol);
        _dataGrid.RenderedColumns.Insert(ageIndex, nameCol);
        _dataGrid.RenderedColumns.RemoveAt(nameIndex);
        _dataGrid.RenderedColumns.Insert(nameIndex, ageCol);
    }
}
